package com.xbongbong.workflow.service.strategy.node.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.config.enums.WorkflowTaskLogStatusEnum;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.OptionalRangeEnum;
import com.xbongbong.paas.exception.WorkflowException;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.help.WorkflowHelp;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.util.WorkflowUtil;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.pro.enums.errorcodes.MessageErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.pro.message.SendMessageUtil;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.PushRefTypePojo;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.service.MessageService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.service.toolbox.help.UserTeamHelp;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserDepartmentEntity;
import com.xbongbong.sys.help.SysUserHelp;
import com.xbongbong.sys.model.DepartmentModel;
import com.xbongbong.sys.model.UserDepartmentModel;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.enums.WorkflowNodeTypeEnum;
import com.xbongbong.workflow.pojo.Data;
import com.xbongbong.workflow.pojo.FlowData;
import com.xbongbong.workflow.pojo.FlowDatas;
import com.xbongbong.workflow.pojo.WorkflowProcessResultPojo;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.pojo.nodes.SendMsgNode;
import com.xbongbong.workflow.pojo.nodes.WorkflowOptionalRangePojo;
import com.xbongbong.workflow.pojo.nodes.WorkflowTitlePojo;
import com.xbongbong.workflow.service.strategy.node.AbstractWorkflowNodeStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 发送消息节点
 * @author 魏荣杰
 *
 */
@Slf4j
@Service
public class WorkflowNodeSendMessageStrategy extends AbstractWorkflowNodeStrategy {

    @Resource
    private MessageService messageService;
    @Resource
    private ProFormHelp proFormHelp;
    @Resource
    private WorkflowHelp workflowHelp;
    @Resource
    private UserTeamHelp userTeamHelp;
    @Resource
    private UserDepartmentModel userDepartmentModel;
    @Resource
    private DepartmentModel departmentModel;
    @Resource
    private PaasMenuModel paasMenuModel;
    @Resource
    private PackageHelp packageHelp;
    @Resource
    private SysUserHelp sysUserHelp;

    @Override
    public String type() {
        return WorkflowNodeTypeEnum.SEND_MESSAGE_NODE.getName();
    }

    @Override
    public void before(WorkflowTransferPOJO workflowTransferPOJO) {
        super.before(workflowTransferPOJO);
    }

    @Override
    public WorkflowProcessResultPojo process(WorkflowTransferPOJO workflowTransferPOJO, WorkflowNodeEntity workflowNodeEntity, Long lastNodeId, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) {
    
        SendMsgNode sendMsgNode = JSON.parseObject(workflowNodeEntity.getConfigData(), SendMsgNode.class);
        if (null == sendMsgNode) {
            log.error("工作流发送消息节点,获取消息失败,不处理");
            return new WorkflowProcessResultPojo(true, false);
        }
        try {
            String corpid = workflowTransferPOJO.getCorpid();
            Integer feeType = packageHelp.getFeeType(corpid);
            if (!Objects.equals(PackageTypeEnum.ULTIMATE.getType(), feeType)) {
                logger(workflowTransferPOJO, workflowNodeEntity, WorkflowTaskLogStatusEnum.PASS, new WorkflowException(MessageConstant.WORKFLOW_NODE_VERSION_NOT_SUPPORT));
                return new WorkflowProcessResultPojo(true, false);
            }
            //通知人
            List<WorkflowOptionalRangePojo> messageRecipient = sendMsgNode.getMessageRecipient();
            if (CollectionsUtil.isEmpty(messageRecipient)) {
                log.error("工作流发送消息节点,消息接收人为空");
                logger(workflowTransferPOJO, workflowNodeEntity, WorkflowTaskLogStatusEnum.FAIL, new WorkflowException(MessageConstant.CC_USER_NOT_SET, ""));
                return new WorkflowProcessResultPojo(true, false);
            }
            List<WorkflowTitlePojo> messageContentList = sendMsgNode.getMessageContent();
            if (CollectionsUtil.isEmpty(messageContentList)) {
                log.warn("工作流发送消息节点,消息内容为空");
                logger(workflowTransferPOJO, workflowNodeEntity, WorkflowTaskLogStatusEnum.PASS, new WorkflowException(MessageConstant.WORKFLOW_NODE_PAAS_MESSAGE_NULL));
                return new WorkflowProcessResultPojo(true, false);
            }
    
            //获取表单数据,防止在for里查询
            Set<Long> workOrder = new HashSet<>();
            Set<Long> others = new HashSet<>();
            Set<Long> workflowNodeIdSet = new HashSet<>();
            // 消息内容
            WorkflowUtil.getFormIdByTitle(messageContentList, workOrder, others, workflowNodeIdSet);
            // 标题
            WorkflowUtil.getFormIdByTitle(sendMsgNode.getTitle(), workOrder, others, workflowNodeIdSet);
            //工单的解释
            Map<Long, Map<String, FieldAttrEntity>> workOrderFormExplainMap = proFormHelp.getWorkOrderFormFieldMap(workOrder, workflowNodeEntity.getCorpid());
            //其他表单的解释
            Map<Long, Map<String, FieldAttrEntity>> formExplainMap = proFormHelp.getFormFieldMap(others, workflowNodeEntity.getCorpid());
            // 获取节点数据，用来拼接内容
            Map<Long, List<PaasFormDataEntityExt>> workflowNodeDataMap = workflowHelp.getWorkflowNodeDataMap(workflowNodeIdSet, workflowTransferPOJO);
            // 拼接消息内容
            String messageContent = workflowHelp.getContentValue(messageContentList, formExplainMap, workOrderFormExplainMap, workflowTransferPOJO, workflowNodeDataMap);
            // 标题
            String title = workflowHelp.getContentValue(sendMsgNode.getTitle(), formExplainMap, workOrderFormExplainMap, workflowTransferPOJO, workflowNodeDataMap);

            // 获取通知人
            Set<String> nodeTaskUserList = getSendUserList(messageRecipient, workflowTransferPOJO, workflowNodeDataMap);
            if (CollectionUtils.isEmpty(nodeTaskUserList)) {
                log.info("工作流发送消息节点,消息接收人为空");
                logger(workflowTransferPOJO, workflowNodeEntity, WorkflowTaskLogStatusEnum.PASS, new WorkflowException(MessageErrorCodeEnum.API_ERROR_380005.getMsg(), ""));
                return new WorkflowProcessResultPojo(true, false);
            }
            // 穿透查看
            Integer viewDetail = sendMsgNode.getViewDetail();
            Long workflowNodeId = sendMsgNode.getWorkflowNodeId();
            Integer triggerNodeFlag = sendMsgNode.getTriggerNodeFlag();
            MessageRabbitMqDTO messageRabbitMqDTO;
            Long menuId;
            PushTypeEnum pushTypeEnum = PushTypeEnum.WORKFLOW_SEND_MESSAGE;
            // 延缓十秒发送
            Long pushTime = DateTimeUtil.getInt()+10;
            if (Objects.equals(BasicConstant.ZERO, viewDetail)) {
                messageRabbitMqDTO = new MessageRabbitMqDTO(workflowNodeEntity.getCorpid(), new ArrayList<>(nodeTaskUserList), Collections.singletonList(0L), 0, pushTime,
                        title, messageContent, null, null);
                // 无需穿透
                messageService.insertPushData(messageRabbitMqDTO, pushTypeEnum);
            } else {
                if (Objects.equals(1, triggerNodeFlag)) {
                    Data data = workflowTransferPOJO.getData();
                    menuId = data.getMenuId();
                    PushRefTypePojo.Options options = SendMessageUtil.getDetailOptions(data.getAppId(), data.getMenuId(), data.getFormId(), data.getId(),
                            data.getSaasMark(), data.getBusinessType(), data.getSubBusinessType());
                    messageRabbitMqDTO = new MessageRabbitMqDTO(workflowNodeEntity.getCorpid(), new ArrayList<>(nodeTaskUserList), Collections.singletonList(data.getId()), 0, pushTime,
                            title, messageContent, null, options);
                } else {
                    FlowData flowData = workflowTransferPOJO.getFlowDatas().getNodeIdMap().get(workflowNodeId.toString());
                    menuId = flowData.getMenuId();
                    List<Long> ids = flowData.getIds();
                    PushRefTypePojo.Options options;
                    if (CollectionsUtil.isEmpty(ids)) {
                        options = null;
                        ids = ids == null ? new ArrayList<>(): ids;
                        ids.add(0L);
                    } else if (ids.size() == BasicConstant.ONE) {
                        options = SendMessageUtil.getDetailOptions(flowData.getAppId(), flowData.getMenuId(), flowData.getFormId(), ids.get(0),
                                flowData.getSaasMark(), flowData.getBusinessType(), flowData.getBusinessType());
                    } else {
                        options = new PushRefTypePojo.Options(flowData.getAppId(), flowData.getMenuId(),
                                flowData.getFormId(), null, flowData.getIds(), flowData.getSaasMark(),
                                flowData.getBusinessType(), flowData.getBusinessType());
                    }
                    messageRabbitMqDTO = new MessageRabbitMqDTO(workflowNodeEntity.getCorpid(), new ArrayList<>(nodeTaskUserList), ids, 0, pushTime,
                            title, messageContent, null, options);
                }
                PaasMenuEntity paasMenuEntity = paasMenuModel.getByKey(menuId, workflowTransferPOJO.getCorpid());
                String name = paasMenuEntity.getName();
                if (messageRabbitMqDTO.getRefIds().size() > BasicConstant.ONE) {
                    messageService.insertListPushData(messageRabbitMqDTO, pushTypeEnum, name);
                } else {
                    Long dataId = messageRabbitMqDTO.getRefIds().get(0);
                    if (Objects.equals(0L, dataId)) {
                        // 无需穿透
                        messageService.insertPushData(messageRabbitMqDTO, pushTypeEnum);
                    } else {
                        messageService.insertDetailPushData(messageRabbitMqDTO, pushTypeEnum, name);
                    }
                }
            }



            // 发送消息成功
            logger(workflowTransferPOJO, workflowNodeEntity, WorkflowTaskLogStatusEnum.PASS, new WorkflowException(MessageConstant.WORKFLOW_NODE_PAAS_MESSAGE_SEND));

        } catch (XbbException e) {
            log.error("工作流发送消息节点,推送mq消息报错", e);
            logger(workflowTransferPOJO, workflowNodeEntity, WorkflowTaskLogStatusEnum.FAIL, new WorkflowException(e.getMsg()));
            return new WorkflowProcessResultPojo(false, false);
        } catch (Exception e) {
            log.error("工作流发送消息节点,推送mq消息报错", e);
            logger(workflowTransferPOJO, workflowNodeEntity, WorkflowTaskLogStatusEnum.FAIL, new WorkflowException(e.getMessage()));
            return new WorkflowProcessResultPojo(false, false);
        }

        return new WorkflowProcessResultPojo(true, false);
    }

    @Override
    public void after(WorkflowTransferPOJO workflowTransferPOJO, WorkflowNodeEntity workflowNodeEntity) {
        super.after(workflowTransferPOJO, workflowNodeEntity);
    }



    @Override
    public void check() {
        super.check();
    }

    @Override
    public void logger(WorkflowTransferPOJO workflowTransferPOJO, WorkflowNodeEntity nodeEntity, WorkflowTaskLogStatusEnum workflowTaskLogStatusEnum, WorkflowException workflowException) {
        super.logger(workflowTransferPOJO, nodeEntity, workflowTaskLogStatusEnum, workflowException);
    }

    /**
     * 获取通知人员
     * @param optionalRangeEntityList
     * @param workflowTransferPOJO
     * @param workflowNodeDataMap
     * @return
     */
    private Set<String> getSendUserList(List<WorkflowOptionalRangePojo> optionalRangeEntityList, WorkflowTransferPOJO workflowTransferPOJO,
                                        Map<Long, List<PaasFormDataEntityExt>> workflowNodeDataMap) {
        // 最后需要新建的节点人员id
        Set<String> userIds = new HashSet<>();
        Map<Long, Map<String, List<String>>> dynamicLevelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Map<String, List<String>>> dynamicUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Set<String> dynamicUserSet = new HashSet<>();
        List<String> roles = new ArrayList<>();
        List<String> departmentList = new ArrayList<>();


        Map<Long, Set<String>> mainUserMap = new HashMap<>(optionalRangeEntityList.size());
        Map<Long, Set<String>> coUserMap = new HashMap<>(optionalRangeEntityList.size());
        // 获取负责人和协同人
        getMainUserAndCoUserList(optionalRangeEntityList, mainUserMap, coUserMap, workflowTransferPOJO, workflowNodeDataMap);

        for (WorkflowOptionalRangePojo workflowOptionalRangePojo : optionalRangeEntityList) {
            if (Objects.equals(workflowOptionalRangePojo.getProperty(), OptionalRangeEnum.WORKFLOW_DYNAMIC_MANAGER.getValue())) {
                Set<String> userSet = new HashSet<>();
                if (Objects.equals(workflowOptionalRangePojo.getAttr(), FieldTypeEnum.OWNERID.getAlias()) && CollectionUtils.isNotEmpty(mainUserMap.get(workflowOptionalRangePojo.getWorkflowNodeId()))) {
                    // 获取负责人
                    userSet.addAll(mainUserMap.get(workflowOptionalRangePojo.getWorkflowNodeId()));
                } else if (Objects.equals(workflowOptionalRangePojo.getAttr(), FieldTypeEnum.COUSERID.getAlias())
                        && CollectionUtils.isNotEmpty(coUserMap.get(workflowOptionalRangePojo.getWorkflowNodeId()))) {
                    // 获取协同人
                    userSet.addAll(coUserMap.get(workflowOptionalRangePojo.getWorkflowNodeId()));
                } else {
                    if (Objects.equals(workflowOptionalRangePojo.getTriggerNodeFlag(), 1)) {
                        JSONObject data = workflowTransferPOJO.getData().getData();
                        Object obj = data.get(workflowOptionalRangePojo.getAttr());
                        if (Objects.nonNull(obj)) {
                            if (obj instanceof List) {
                                List<String> userList = JSON.parseArray(JSON.toJSONString(data.get(workflowOptionalRangePojo.getAttr())), String.class);
                                userSet.addAll(userList);
                            } else {
                                userSet.add(data.getString(workflowOptionalRangePojo.getAttr()));
                            }
                        }
                    } else {
                        List<PaasFormDataEntityExt> list = workflowNodeDataMap.get(workflowOptionalRangePojo.getWorkflowNodeId());
                        if (CollectionsUtil.isNotEmpty(list)) {
                            list.forEach(item->{
                                if (Objects.equals(workflowOptionalRangePojo.getAttr(), FieldTypeEnum.CREATORID.getAlias())) {
                                    userSet.add(item.getCreatorId());
                                } else {
                                    JSONObject data = item.getData();
                                    Object obj = data.get(workflowOptionalRangePojo.getAttr());
                                    if (Objects.nonNull(obj)) {
                                        if (obj instanceof List) {
                                            List<String> userList = JSON.parseArray(JSON.toJSONString(data.get(workflowOptionalRangePojo.getAttr())), String.class);
                                            userSet.addAll(userList);
                                        } else {
                                            userSet.add(data.getString(workflowOptionalRangePojo.getAttr()));
                                        }
                                    }
                                }
                            });
                        }
                    }
                }
                if (CollectionUtils.isNotEmpty(userSet)) {


                    if (dynamicUserMap.containsKey(workflowOptionalRangePojo.getWorkflowNodeId())) {
                        Map<String, List<String>> userMap = dynamicUserMap.get(workflowOptionalRangePojo.getWorkflowNodeId());
                        userMap.computeIfAbsent(workflowOptionalRangePojo.getAttr(), v -> new ArrayList<>(userSet));
                        dynamicUserMap.put(workflowOptionalRangePojo.getWorkflowNodeId(), userMap);
                    } else {
                        Map<String, List<String>> userMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        userMap.computeIfAbsent(workflowOptionalRangePojo.getAttr(), v -> new ArrayList<>(userSet));
                        dynamicUserMap.put(workflowOptionalRangePojo.getWorkflowNodeId(), userMap);
                    }
                    dynamicUserSet.addAll(userSet);
                    if (dynamicLevelMap.containsKey(workflowOptionalRangePojo.getWorkflowNodeId())) {
                        Map<String, List<String>> levelMap = dynamicLevelMap.get(workflowOptionalRangePojo.getWorkflowNodeId());
                        levelMap.computeIfAbsent(workflowOptionalRangePojo.getAttr(), v -> Collections.singletonList(workflowOptionalRangePojo.getId()));
                        dynamicLevelMap.put(workflowOptionalRangePojo.getWorkflowNodeId(), levelMap);
                    } else {
                        Map<String, List<String>> levelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        levelMap.computeIfAbsent(workflowOptionalRangePojo.getAttr(), v -> Collections.singletonList(workflowOptionalRangePojo.getId()));
                        dynamicLevelMap.put(workflowOptionalRangePojo.getWorkflowNodeId(), levelMap);
                    }
                }
            } else if (Objects.equals(workflowOptionalRangePojo.getProperty(), OptionalRangeEnum.USER.getValue())) {
                userIds.add(workflowOptionalRangePojo.getId());
            } else if (Objects.equals(workflowOptionalRangePojo.getProperty(), OptionalRangeEnum.ROLE.getValue())) {
                roles.add(workflowOptionalRangePojo.getId());
            } else if (Objects.equals(workflowOptionalRangePojo.getProperty(), OptionalRangeEnum.DEPT.getValue())) {
                departmentList.add(workflowOptionalRangePojo.getId());
            }
        }


        // 动态人员处理
        if (!dynamicUserSet.isEmpty()) {
            List<DepartmentEntity> allDepartment = new ArrayList<>();
            try {
                allDepartment = departmentModel.findAllDepartment(workflowTransferPOJO.getCorpid(), true);
            } catch (Exception e) {
                log.error("workflowNodeSendMessageStrategy.getSendUserList.error：", e);
            }
            // 所有部门的id和部门的map
            Map<Long, DepartmentEntity> departMap = new HashMap<>();
            if (Objects.nonNull(allDepartment)) {
                allDepartment.forEach(v -> departMap.put(v.getId(), v));
            }
            // 顶级主管
            DepartmentEntity topDepart = departMap.get(1L);
            List<String> topUserIds = userDepartmentModel.getDepManagerList(topDepart);


            Map<String, List<UserDepartmentEntity>> userDepartMap = sysUserHelp.getUserDepartmentMap(workflowTransferPOJO.getCorpid(), dynamicUserSet);

            Set<Long> deptList = new HashSet<>();
            dynamicUserMap.forEach((key, value)->{
                Map<String, List<String>> levelMap = dynamicLevelMap.get(key);
                value.forEach((attr, users)->{
                    List<String> levels = levelMap.get(attr);
                    for (String userId : users) {
                        List<UserDepartmentEntity> userDepartmentEntities = userDepartMap.getOrDefault(userId, new ArrayList<>());
                        sysUserHelp.getUserIdAndDepartmentId(userDepartmentEntities, departMap, levels, userIds, userId, topUserIds, deptList);
                    }
                });
            });

            userIds.addAll(sysUserHelp.getUserIdIsLeader(workflowTransferPOJO.getCorpid(), deptList));
        }
        // 去除离职人员
        Set<String> userIdList = sysUserHelp.getUserIdList(workflowTransferPOJO.getCorpid(), userIds);

        // role的处理
        userIdList.addAll(sysUserHelp.getUserIdListByRole(workflowTransferPOJO.getCorpid(), roles));

        // department的处理
        userIdList.addAll(sysUserHelp.getUserIdListByDepartment(workflowTransferPOJO.getCorpid(), departmentList));

        return userIdList;
    }

    /**
     * 获取协同人和负责人
     * @param optionalRangeEntityList
     * @param mainUserMap
     * @param coUserMap
     * @param workflowTransferPOJO
     * @param workflowNodeDataMap
     */
    private void getMainUserAndCoUserList(List<WorkflowOptionalRangePojo> optionalRangeEntityList,  Map<Long, Set<String>> mainUserMap,
                                 Map<Long, Set<String>> coUserMap, WorkflowTransferPOJO workflowTransferPOJO,  Map<Long, List<PaasFormDataEntityExt>> workflowNodeDataMap) {
        Data workflowData = workflowTransferPOJO.getData();
        JSONObject data = workflowTransferPOJO.getData().getData();
        String corpid = workflowTransferPOJO.getCorpid();
        optionalRangeEntityList.forEach(item->{
            if (Objects.equals(item.getProperty(), OptionalRangeEnum.WORKFLOW_DYNAMIC_MANAGER.getValue())) {
                if (Objects.equals(item.getAttr(), FieldTypeEnum.OWNERID.getAlias())) {
                    if (Objects.equals(BasicConstant.ONE, item.getTriggerNodeFlag())) {
                        Object object = data.get(item.getAttr());
                        if (object == null) {
                            return;
                        }
                        if (UserTeamEnum.hasTeam(workflowData.getBusinessType())) {
                            List<UserTeamEntity> userList = userTeamHelp.getUserTeamList(Arrays.asList(workflowData.getId()), corpid, workflowData.getBusinessType(), false, 1);
                            Set<String> userSet = new HashSet<>();
                            userList.forEach(user->userSet.add(user.getUserId()));
                            mainUserMap.put(item.getWorkflowNodeId(), userSet);
                        } else {
                            mainUserMap.put(item.getWorkflowNodeId(), Collections.singleton(data.getString(item.getAttr())));
                        }
                    } else {
                        FlowDatas flowDatas = workflowTransferPOJO.getFlowDatas();
                        FlowData flowData = flowDatas.getNodeIdMap().get(item.getWorkflowNodeId().toString());
                        if (Objects.isNull(flowData)) {
                            return;
                        }
                        if (UserTeamEnum.hasTeam(flowData.getBusinessType())) {
                            List<UserTeamEntity> userList = userTeamHelp.getUserTeamList(flowData.getIds(), corpid, flowData.getBusinessType(), false, 1);
                            Set<String> userSet = new HashSet<>();
                            userList.forEach(user->userSet.add(user.getUserId()));
                            mainUserMap.put(item.getWorkflowNodeId(), userSet);
                        } else {
                            Set<String> userSet = new HashSet<>();
                            List<PaasFormDataEntityExt> list = workflowNodeDataMap.get(item.getWorkflowNodeId());
                            list.forEach(formData->{
                                if (StringUtil.isNotEmpty(formData.getOwnerId())) {
                                    userSet.add(formData.getOwnerId());
                                }
                            });
                            mainUserMap.put(item.getWorkflowNodeId(), userSet);
                        }
                    }
                } else if (Objects.equals(item.getAttr(), FieldTypeEnum.COUSERID.getAlias())) {
                    if (Objects.equals(BasicConstant.ONE, item.getTriggerNodeFlag())) {
                        Object object = data.get(item.getAttr());
                        if (object == null) {
                            return;
                        }
                        if (UserTeamEnum.hasTeam(workflowData.getBusinessType())) {
                            List<UserTeamEntity> userList = userTeamHelp.getUserTeamList(Arrays.asList(workflowData.getId()), corpid, workflowData.getBusinessType(), false, 0);
                            Set<String> userSet = new HashSet<>();
                            userList.forEach(user->userSet.add(user.getUserId()));
                            coUserMap.put(item.getWorkflowNodeId(), userSet);
                        }else {
                            coUserMap.put(item.getWorkflowNodeId(), Collections.singleton(data.getString(item.getAttr())));
                        }
                    } else {
                        FlowDatas flowDatas = workflowTransferPOJO.getFlowDatas();
                        FlowData flowData = flowDatas.getNodeIdMap().get(item.getWorkflowNodeId().toString());
                        if (Objects.isNull(flowData)) {
                            return;
                        }
                        if (UserTeamEnum.hasTeam(flowData.getBusinessType())) {
                            List<UserTeamEntity> userList = userTeamHelp.getUserTeamList(flowData.getIds(), corpid, flowData.getBusinessType(), false, 0);
                            Set<String> userSet = new HashSet<>();
                            userList.forEach(user->userSet.add(user.getUserId()));
                            coUserMap.put(item.getWorkflowNodeId(), userSet);
                        } else {
                            // TODO 协同人暂时没有
                        }
                    }
                }
            }
        });
    }

}
